/**
    audioplugin.c - Woofs plugin for handling tagged audio files.
    Copyright (C) 2009  Abram Magner

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include <taglib/tag_c.h>
#include "woofs_plugin.h"

static void audio_init (void);
static void audio_fini (void);
static int audio_claim_file (int, char *);
static void audio_get_tags (int, char *, void *, tag_set_filler);

static char *unicode_to_ascii (unsigned long int *);
static int is_suffix (char *, char *);

woofs_format_plugin woofs_struct = 
{
  WOOFS_PLUGIN_MAGIC,
  audio_init,
  audio_fini,
  audio_claim_file,
  audio_get_tags
};


static void audio_init (void)
{
}

static void audio_fini (void)
{
}

static int audio_claim_file (int fd, char *filepath)
{
  TagLib_File *handle = NULL;
  int isvalid = 0;
  handle = taglib_file_new (filepath);
  if (handle == NULL) return (0);
  isvalid = taglib_file_is_valid (handle);
  taglib_file_free (handle);
  return (isvalid);
}


static void audio_get_tags (int fd, char *filepath, void *tagset, tag_set_filler filler)
{
  TagLib_File *handle = NULL;
  TagLib_Tag *tag = NULL;
  char *artist = NULL, *album = NULL, *comment = NULL, *genre = NULL;
  unsigned int year = 0, track = 0;
  close (fd);

  handle = taglib_file_new (filepath);
  if (handle == NULL) return;
  tag = taglib_file_tag (handle);

  artist = taglib_tag_artist (tag);
  album = taglib_tag_album (tag);
  comment = taglib_tag_comment (tag);
  genre = taglib_tag_genre (tag);

  filler (tagset, "artist", artist);
  filler (tagset, "album", album);
  filler (tagset, "comment", comment);
  filler (tagset, "genre", genre);

  taglib_file_free (handle);
}

//=============================================================================
// Convert a null-terminated unicode string to ascii.
// FIXME: This might not be portable!
//=============================================================================
static char *unicode_to_ascii (unsigned long int *str)
{
  int i = 0;
  char *ptr = (char *)str;
  char *newstr = NULL;
  int cursize = 32;

  newstr = malloc (cursize * sizeof (char));
  if (newstr == NULL) return (NULL);
  while (*str != 0)
    {
      if (i >= cursize - 1)
	{
	  char *temp = NULL;
	  cursize *= 2;
	  temp = realloc (newstr, cursize);
	  if (temp == NULL)
	    {
	      free (newstr);
	      return (NULL);
	    }
	  newstr = temp;
	}
      ptr = (char *)str;
      newstr[i] = *ptr;
      str++;
      i++;
    }
  newstr[i] = '\0';
  return (newstr);
}


//=============================================================================
//  Return true if suff is a suffix of str.
//=============================================================================
static int is_suffix (char *str, char *suff)
{
  int iter = 0, suffiter = 0, len = strlen (str), sufflen = strlen (suff);

  if (len < sufflen) return (0);
  suffiter = sufflen - 1;
  for (iter = len - 1; iter >= len - sufflen; iter--, suffiter--)
    if (str[iter] != suff[suffiter]) return (0);
  return (1);
}
